<?php
declare (strict_types=1);

namespace app\api\controller;

use Alipay\EasySDK\Kernel\Factory;
use app\common\tool\pay\Pay;
use app\common\tool\pay\PayPreset;
use app\common\model\Logs;
use app\common\model\Order;
use app\common\model\User;
use app\common\facade\OssBackup;

class Callback
{

    //自动备份
    public function ossBackup()
    {
        try {
            $route = 'callback/autoOssBackup.log';
            $result = OssBackup::auto();
            Logs::write($route, json_encode($result));
        } catch (\Exception $e) {
            Logs::write($route, $e->getMessage());
        }
    }
    /**
     * @return string|void
     * 原点 2022年1月11日11:27:15 支付宝支付通用回调
     */
    public function aliPayOrder()
    {
        try {
            Factory::setOptions(Pay::getOptions(PayPreset::Instance()->getConfig()['alipay']));
            $route = 'callback/aliPayOrder.log';
            $param = $_POST;
            $result = Factory::payment()->common()->verifyNotify($param);
            if ($result) {
                if ($param['trade_status'] != 'TRADE_SUCCESS') {
                    Logs::write($route, '非支付回调');
                    exit();
                }
                $info = Order::get(['sn' => $param['out_trade_no']]);
                if ($info && $info->state == 1) {
                    $result = Order::operation($info, User::find($info['uid']), 'alipay');
                    if ($result['code'] == 1) {
                        Logs::write($route, '成功，订单号:' . $info->sn);
                    } else {
                        Logs::write($route, '失败，订单号:' . $info->sn . '->' . $result['msg']);
                    }
                } else {
                    Logs::write($route, '未查询到订单');
                }

                return 'success';
            } else {
                return 'fail';
            }
        } catch (\Exception $e) {
            return 'fail';
        }
    }

    /**
     * @return string
     * 原点 2022年1月11日11:32:49 微信支付通用回调
     */
    public function weChatOrder()
    {
        try {
            $wechat = \WeChat\Pay::instance(PayPreset::Instance()->getConfig()['wechat']);
            $data = $wechat->getNotify();
            $route = 'callback/weChatOrder.log';
            if ($data['return_code'] === 'SUCCESS' && $data['result_code'] === 'SUCCESS') {
                $info = Order::get(['sn' => $data['out_trade_no']]);
                if ($info && $info->state == 1) {
                    $result = Order::operation($info, User::find($info['uid']), 'alipay');
                    if ($result['code'] == 1) {
                        Logs::write($route, '成功，订单号:' . $info->sn);
                    } else {
                        Logs::write($route, '失败，订单号:' . $info->sn . '->' . $result['msg']);
                    }
                } else {
                    Logs::write($route, '未查询到订单');
                }
            }else{
                Logs::write($route, '非支付回调');
                exit();
            }
            return 'success';
        } catch (\Exception $e) {
            return 'fail';
        }
    }
}